package xuhaijia.lanou3g.autohome.turnpage;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.activity.WebViewActivity;
import xuhaijia.lanou3g.autohome.find.FindFragment;
import xuhaijia.lanou3g.autohome.find.TurnBean;
import xuhaijia.lanou3g.autohome.recommend.recommendreusing.RecommendBean;

/**
 * Created by 7 on 16/8/19.
 */
public class TurnPageCenter {
    private Context context;
    private TurnBean turnBean;
    private View view;
    private TurnPagerRightDownAdapter turnPagerRightDownAdapter;
    private ViewPager turnVp;
    private ImageView[] tips;
    private LinearLayout ll;
    private Count time;
    FindFragment findFragment;

    public void setFindFragment(FindFragment findFragment) {
        this.findFragment = findFragment;
    }

    public TurnPageCenter(Context context) {
        this.context = context;
    }

    public void setTurnBean(TurnBean turnBean) {
        this.turnBean = turnBean;
    }

    public View getView() {
        TurnPageCenterAdapter adapter = new TurnPageCenterAdapter(context);
        view = LayoutInflater.from(context).inflate(R.layout.turn_vp_center, null);
        turnVp = (ViewPager) view.findViewById(R.id.turnpager_vp_center);
        turnVp.setCurrentItem(6000, false);
        turnVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < tips.length; i++) {
                    if (i == position % tips.length) {
                        // 选中的
                        tips[i].setImageResource(R.drawable.shape_point_white);
                    } else {
                        // 未选中
                        tips[i].setImageResource(R.drawable.shape_point_gray);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        time = new Count(Integer.MAX_VALUE, 5000);
        time.start();
        ll = (LinearLayout) view.findViewById(R.id.ll_center);
        indicator(turnBean);
        adapter.setTurnBean(turnBean);
        adapter.setPagerClickListener(new TurnPageCenterAdapter.PagerClickListener() {
            @Override
            public void pagerClickListener(int position) {
                int size = turnBean.getResult().getList().size();
                Intent intent = new Intent();
                intent.putExtra("url", turnBean.getResult().getList().get(position % size).getUrl());
                findFragment.setPagerIntent(intent);
            }
        });
        turnVp.setAdapter(adapter);
        return view;
    }

    public void stop() {
        if (time != null) {
            time.cancel();
        }
    }

    private void indicator(TurnBean turnBean) {
        tips = new ImageView[turnBean.getResult().getList().size()];
        for (int i = 0; i < turnBean.getResult().getList().size(); i++) {
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(40, 7));
            tips[i] = imageView;
            if (i == 0) {
                imageView.setImageResource(R.drawable.shape_point_white);

            } else {
                imageView.setImageResource(R.drawable.shape_point_gray);
            }
            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(
                            40, 7);
            layoutParams.leftMargin = 10;
            layoutParams.rightMargin = 10;
            ll.addView(imageView, layoutParams);
        }

    }

    private class Count extends CountDownTimer {
        public Count(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            turnVp.setCurrentItem(turnVp.getCurrentItem() + 1);
        }

        @Override
        public void onFinish() {
        }
    }
}

